<template>
	<view class="con-body u-wrap">
		<view class="">
			<!-- 2.0.19支持autoBack，默认为false -->
			<u-navbar leftText="选择工具" autoBack>
			</u-navbar>
		</view>
		<view style="padding: 0 20rpx; background-color: #FFFFFF;">
			<view class="example-body">
				<uni-datetime-picker v-model="datetimerange" start-placeholder="开始时间" end-placeholder="结束时间" type="datetimerange" rangeSeparator="至" />
			</view>
		</view>
		<u-line></u-line>
		<view class="u-menu-wrap">

			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
					:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
					@tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<block v-for="(item,index) in tabbar" :key="index">
				<scroll-view scroll-y class="right-box" v-if="current==index">
					<view class="page-view">
						<view class="class-item">
							<view class="item-title">
								<text>{{item.name}}</text>
							</view>
							<view class="item-container">
								<view class="thumb-box" v-for="(item1, index1) in item.foods" :key="index1">

									<view
										style="height: 60rpx; line-height: 60rpx; background-color: #eeeeee; margin-top: 10rpx; border-radius: 10rpx; padding-left: 30rpx;">
										{{item1.name}}
										<view v-if="item1.stock == 0" style="float: right; margin: 10rpx 30rpx;">
											<text>库存不足</text>
										</view>
										<view v-if="item1.status == 1" style="float: right; margin: 10rpx 30rpx;"
											@click="plus(item1)">
											<u-icon name="plus-circle" color="#606266" size="24"></u-icon>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
			<view class="uni-cell-90" style="width: 100%; position: fixed; bottom: 40rpx;">
				<view class="iconType2">
					<view class="text1" style="margin-top:0;">
						<view class="dis_inl_blo">
							<u-icon name="https://resourse.cnlhjt.com/upload/20220825/1d7402394dc492783df102f7a89bee2b.png" label="工具箱"></u-icon>
						</view>
						
						<text v-if="num>0" class="num_con">{{num}}</text>
					</view>
					<view class="text2" @click="next">
						下一步
					</view>
				</view>
			</view>
		</view>
			<u-toast ref="uToast" :loading="true"></u-toast>
			
		<u-calendar :show="showTime" mode="range" @confirm="timeSelect" @close="showTime=false"></u-calendar>
	</view>
</template>

<script>
	import { getTools } from "@/service/shareService.js";
	export default {
		data() {
			return {
				showTime: false,
				model1: {
					time: '',
					nid: [],
					tools: [],
					start_time: '',
					end_time: ''
				},
				tabbar: [],
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度

				num: 0,
				
				datetimerange: [],
			}
		},
		watch:{
			datetimerange(newval) {
				if(this.datetimerange.length>0){
					this.model1.time = this.datetimerange[0] + '~' + this.datetimerange[1];
					this.model1.start_time = this.datetimerange[0];
					this.model1.end_time = this.datetimerange[1];
				}else{
					this.model1.time = '';
					this.model1.start_time = '';
					this.model1.end_time = '';
				}
				
			}
		},
		computed: {
			
		},
		onLoad() {
			getTools().then(this.getTools);
		},
		methods: {
			getTools(e){
				if(e.code == 200){
					this.tabbar = e.result;
				}
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			plus(item1) {
				// console.log(item1);
				var a = this.model1.nid.indexOf(item1.cat);
				if(a > -1){
					this.$refs.uToast.show({
					
						type: 'error',
						message: '请勿重复点击'
					})
				}else{
					this.num++;
					this.model1.nid.push(item1.cat);
					this.model1.tools.push(item1.name);
				}
			},
			minus(index) {
				this.num--;
				this.toolsList[index].status = 0;
			},
			next() {
				// console.log(this.model1);
				if(this.model1.time == ''){
					this.$refs.uToast.show({
					
						type: 'error',
						message: '请选择预约时间'
					})
					return;
				}
				this.$u.route({
					url: "/pagesShare/tool/tool-detail",
					params: {
						item: JSON.stringify(this.model1)
					}
				})
			},
			timeSelect(e) {
				this.model1.time = e[0] + '~' + e[e.length - 1];
				
				this.model1.start_time = e[0];
				this.model1.end_time = e[e.length - 1];
				this.showTime = false;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}
	.example-body {
		padding:10rpx 0;
		background-color: #fff;
	}
	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid red;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 16rpx;
	}

	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}

	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}

	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 100%;
		display: flex;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
		color: #333333;
	}

	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}

	.iconType2 {
		position: fixed;
		bottom: 60rpx;
		display: flex;
		border: #E1E1E1 1rpx solid;
		border-radius: 40rpx;
		justify-content: space-between;
		width: 90%;
		margin-left: 5%;
		height: 80rpx;
		line-height: 80rpx;
		background-color: #FFFFFF;

		.text1 {
			color: red;
			margin-top: 27rpx;
			width: 30%;
			margin-left: 200rpx;
		}

		.text2 {
			color: #FFFFFF;
			text-align: center;
			width: 30%;
			background-image: url("https://resourse.cnlhjt.com/upload/20220825/79b8644697f64c4b587f78e6d891f1e8.png");
			background-size: 100% 100%;
		}
	}
	.num_con{
		position: relative;
		top:-18rpx;
		left:-5rpx;
		display: inline-block;
		width:35rpx;
		height: 35rpx;
		line-height: 35rpx;
		text-align: center;
		color:#fff;
		font-size: 26rpx;
		background-color: #f00;
		border-radius: 50%;
	}
</style>
